package com.sinosoft.data;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.sinosoft.base.Constants;
import com.sinosoft.base.GenericDaoHibernate;
import com.sinosoft.product.ZywxImage0;
import com.sinosoft.product.ZywxImage1;
import com.sinosoft.quick.Quick;
import com.sinosoft.util.SystemLogUtil;

@Repository
public class ZywxImage0DaoHibernate extends
		GenericDaoHibernate<ZywxImage0, Long> implements ZywxImage0Dao {

	public ZywxImage0DaoHibernate() {
		super(ZywxImage0.class);
	}

	public int getPageSize(String sql) {
		String strsql = "select count(*) from ZYWX_IMAGE_0  " + sql + "order by recorddate desc";
		int pageCount = 0;
		Session session = null;
		try {
			session = this.getHibernateTemplate().getSessionFactory().openSession();
			Query query = session.createSQLQuery(strsql);
			List list = query.list();
		
			pageCount = Integer.valueOf(list.get(0).toString());
		} catch (Exception e) {
			SystemLogUtil.saveError(Constants.LOG_ERROR, e.getMessage());
		} finally {
			session.close();
		}	
        return pageCount;
		
	}

	public List<ZywxImage0> getZywxImage0() {
		
		return getHibernateTemplate().find("from ZywxImage0 zywx0");
	}

	public List<ZywxImage0> getZywxImage0(int pageFirst, int pageSize) {
		List<ZywxImage0> list = null;
		Session session = null;
		try {
			session = this.getHibernateTemplate().getSessionFactory()
					.openSession();
			Criteria c = session.createCriteria(ZywxImage0.class);
			c.setFirstResult(pageFirst);
			c.setMaxResults(pageSize);
			list = c.list();

		} catch (Exception e) {
			SystemLogUtil.saveError(Constants.LOG_ERROR, e.getMessage());
		} finally {
			session.close();

		}
		return list;
		
	}
	//通过id 查询出一条记录
	public ZywxImage0 getZywxImage0ById(BigDecimal scenenum) {
		
		List zywxlist = getHibernateTemplate().find("from ZywxImage0 where scenenum=?",scenenum);
		if(zywxlist == null ||zywxlist.isEmpty()){
			return null;
		}else{
			return (ZywxImage0)zywxlist.get(0);
		}
		
	}

	public List<ZywxImage0> getZywxImage0List(int pageFirst, int pageSize,
			ZywxImage0 zywx0) {
		List<ZywxImage0> list =  null;
		String  Sensorid = zywx0.getSensorid();
		String   Satelliteid = zywx0.getSatelliteid();
		Session session = null;
		Criteria c = null;
		
		try{
			session = this.getHibernateTemplate().getSessionFactory().openSession();
			
			c = session.createCriteria(ZywxImage0.class);
			if(!"".equalsIgnoreCase(Sensorid)){
				c.add(Restrictions.like("Sensorid", "%" + Sensorid + "%"));
			}
			if(!"".equalsIgnoreCase(Satelliteid)){
				c.add(Restrictions.like("Satelliteid", "%" + Satelliteid + "%"));
			}
			c.setFirstResult(pageFirst);
			c.setMaxResults(pageSize);
			list = c.list();
		}catch(Exception e){
			SystemLogUtil.saveError(Constants.LOG_ERROR, e.getMessage());
		}finally{
			session.close();
			
		}
		return list;
		
	}

	public void removeZywxImage0(BigDecimal scenenum) {
		ZywxImage0 zywx = new ZywxImage0();
		List zywxlist = getHibernateTemplate().find("from ZywxImage0 where scenenum=?",scenenum);
		if(zywxlist == null ||zywxlist.isEmpty()){
			
		}else{
			zywx = (ZywxImage0) zywxlist.get(0);
		}
		getHibernateTemplate().delete(zywx);
		getHibernateTemplate().flush();
		
	}

	public ZywxImage0 saveZywxImage0(ZywxImage0 zywx0) {
		
		getHibernateTemplate().saveOrUpdate(zywx0);
 		getHibernateTemplate().flush();
		return zywx0;
		/*if(this.getZywxImage0Bythree(zywx0.getSatelliteid(),zywx0.getSensorid(),zywx0.getScenenum()) ==0){
			 getHibernateTemplate().save(zywx0);
				
		}else{
			getHibernateTemplate().merge(zywx0);
			getHibernateTemplate().flush();
		}
		return zywx0;*/
		
	}

	public ZywxImage0 getZywxImageBySensorid(String sensorid) {
		String hqls = "from ZywxImage0 where sensorid='"+sensorid+"'";
		List<ZywxImage0> zywxImagelist = this.getHibernateTemplate().find(hqls);
		
		return zywxImagelist!=null && zywxImagelist.size()>0?zywxImagelist.get(0):null;
	}

	//联合主键
    /* public int getZywxImage0Bythree(String satelliteid,String sensorid,BigDecimal scenenum) {
		
		List zywxlist = getHibernateTemplate().find("from ZywxImage0 where  satelliteid=? and sensorid=? and scenenum=?",satelliteid,sensorid,scenenum);
		if(zywxlist == null ||zywxlist.isEmpty()){
			return 0;
		}else{
			return 1;
		}
		
	}*/

}
